package org.zkzd.helloEasyQuery.common.config;

import com.easy.query.api.proxy.client.EasyEntityQuery;
import com.easy.query.core.basic.api.database.CodeFirstCommand;
import com.easy.query.core.basic.api.database.DatabaseCodeFirst;
import org.springframework.context.annotation.Configuration;
import org.zkzd.helloEasyQuery.entity.Company;
import org.zkzd.helloEasyQuery.entity.SysUser;

import java.util.Arrays;

@Configuration
public class AppConfiguration {

    public AppConfiguration(EasyEntityQuery easyEntityQuery){

        DatabaseCodeFirst databaseCodeFirst = easyEntityQuery.getDatabaseCodeFirst();
        //如果不存在数据库则创建
        databaseCodeFirst.createDatabaseIfNotExists();
        //自动同步数据库表
        CodeFirstCommand codeFirstExecutable = databaseCodeFirst.syncTableCommand(Arrays.asList(Company.class, SysUser.class));
        //执行命令
        codeFirstExecutable.executeWithTransaction(arg -> {
            System.out.println(arg.getSQL());
            arg.commit();
        });

    }
}